package com.huixue.cn.ztej.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.huixue.cn.ztej.base.Constant;
import com.huixue.cn.ztej.bean.Menu;
import com.huixue.cn.ztej.dao.MenuDAO;
import com.huixue.cn.ztej.service.IMenuService;
import com.huixue.cn.ztej.utils.Strings;

@Service
@Scope("singleton")
public class MenuServiceImpl implements IMenuService {

	@Resource(name = "menuDAO")
	private MenuDAO dao;

	private void saveMenu(Menu menu) {
		menu.setMenuId(Strings.uuid());
		menu.setCreateTime(new Date());
		menu.setUpdateTime(new Date());
		dao.saveMenu(menu);
	}

	private void updateMenu(Menu menu) {
		menu.setUpdateTime(new Date());
		dao.updateMenu(menu);
	}

	@Override
	public void updateInfo(String menuId, String menuType, String menuKey,
			String menuUrl) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("menuId", menuId);
		map.put("menuType", menuType);
		map.put("menuKey", menuKey);
		map.put("menuUrl", menuUrl);
		map.put("updateTime", new Date());
		dao.updateInfo(map);
	}

	@Override
	public boolean isMenuNameExisted(String systemId, String menuName) {
		List<Menu> list = dao.getMenuByCondition(systemId, menuName);
		if (list != null && list.size() > 0) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public void changeOrder(String menuId, String menuOrder, String prevId,
			String prevOrder) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("menuId", menuId);
		map.put("menuOrder", prevOrder);
		map.put("updateTime", new Date());
		dao.changeOrder(map);
		map.put("menuId", prevId);
		map.put("menuOrder", menuOrder);
		dao.changeOrder(map);
	}

	@Override
	public void saveOrUpdateMenu(Menu menu) {
		if (menu != null) {
			if (StringUtils.isEmpty(menu.getMenuId())) {
				saveMenu(menu);
			} else {
				updateMenu(menu);
			}
		}
	}

	@Override
	public Menu getMenuById(String menuId) {
		return dao.getMenuById(menuId);
	}

	@Override
	public List<Menu> getMenuBySystemId(String systemId) {
		return dao.getMenuBySystemId(systemId);
	}

	@Override
	public List<Menu> getMenuByParent(String systemId, String parent) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("systemId", systemId);
		map.put("parent", parent);
		return dao.getMenuByParent(map);
	}

	@Override
	public void deleteMenu(String menuId) {
		dao.deleteMenu(menuId);
	}

	@Override
	public void deleteMenuByParent(String parent) {
		dao.deleteMenuByParent(parent);
	}

	/**
	 * 获得菜单组成的json
	 */
	@Override
	public String getMenuJsonBySystemId(String systemId) {
		List<Menu> menuList = getMenuBySystemId(systemId);
		StringBuffer sb = new StringBuffer();
		sb.append(" {    \"button\":[");
		boolean have = false;
		for (Menu menu : menuList) {
			if (Constant.MODEL_PARENT_SUPER.equals(menu.getParent())) {
				have = true;
				sb.append("{");
				sb.append("\"name\":");
				sb.append("\"" + menu.getMenuName() + "\",");
				if (StringUtils.isEmpty(menu.getMenuType())) {
					if (haveChild(menu.getMenuId(), menuList)) {
						addChildMenu(menu.getMenuId(), menuList, sb);
					} else {
						sb.replace(sb.length() - 1, sb.length(), "");
					}
				} else if (Constant.MENU_TYPE_CLICK.equals(menu.getMenuType())) {
					if (haveChild(menu.getMenuId(), menuList)) {
						addChildMenu(menu.getMenuId(), menuList, sb);
					} else {
						sb.append("\"type\":");
						sb.append("\"click\",");
						sb.append("\"key\":");
						sb.append("\"" + menu.getMenuKey() + "\"");
					}
				} else if (Constant.MENU_TYPE_URL.equals(menu.getMenuType())) {
					if (haveChild(menu.getMenuId(), menuList)) {
						addChildMenu(menu.getMenuId(), menuList, sb);
					} else {
						sb.append("\"type\":");
						sb.append("\"view\",");
						sb.append("\"url\":");
						sb.append("\"" + menu.getMenuUrl() + "\"");
					}
				}
				sb.append("}");
				sb.append(",");
			}
		}
		if (have) {
			sb.replace(sb.length() - 1, sb.length(), "");
		}

		sb.append("]}");
		return sb.toString();
	}

	/**
	 * 添加子节点信息
	 * 
	 * @param parent
	 * @param menuList
	 * @param sb
	 */
	private void addChildMenu(String parent, List<Menu> menuList,
			StringBuffer sb) {
		sb.append("\"sub_button\":[");
		boolean have = false;
		for (Menu menu : menuList) {
			if (parent.equals(menu.getParent())) {
				have = true;
				sb.append("{");
				sb.append("\"name\":");
				sb.append("\"" + menu.getMenuName() + "\",");
				if (StringUtils.isEmpty(menu.getMenuType())) {
					sb.append("\"type\":");
					sb.append("\"\",");
					sb.append("\"key\":");
					sb.append("\"\"");
				} else if (Constant.MENU_TYPE_CLICK.equals(menu.getMenuType())) {
					sb.append("\"type\":");
					sb.append("\"click\",");
					sb.append("\"key\":");
					sb.append("\"" + menu.getMenuKey() + "\"");
				} else if (Constant.MENU_TYPE_URL.equals(menu.getMenuType())) {
					sb.append("\"type\":");
					sb.append("\"view\",");
					sb.append("\"url\":");
					sb.append("\"" + menu.getMenuUrl() + "\"");
				}
				sb.append("}");
				sb.append(",");
			}
		}
		if (have) {
			sb.replace(sb.length() - 1, sb.length(), "");
		}
		sb.append("]");
	}

	private boolean haveChild(String parent, List<Menu> menuList) {
		boolean status = false;
		for (Menu menu : menuList) {
			if (parent.equals(menu.getParent())) {
				status = true;
				break;
			}
		}
		return status;
	}
}
